package com.example.minefieldkeepercloud.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.adapter.MinerHarvesAdapter;
import com.example.minefieldkeepercloud.adapter.StaffAdapter;
import com.example.minefieldkeepercloud.bean.LineChartTestBean;
import com.example.minefieldkeepercloud.bean.MinerHarvestBean;
import com.example.minefieldkeepercloud.bean.StaffBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.interfac.Observer;
import com.example.minefieldkeepercloud.interfac.OnSelectParameterListener;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.DynamicLineChartManager;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.lazyviewpager.LazyFragmentPagerAdapter;
import com.example.minefieldkeepercloud.view.pop.TypeSelectPopupWindow;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 矿机算力统计
 *
 * @Author:Created by  ygy
 * @time:2019/12/2
 */
public class MinerHarvestFragment extends Fragment implements  LazyFragmentPagerAdapter.Laziable{


    @BindView(R.id.recyleview)
    LRecyclerView mRecyclerView;

    @BindView(R.id.tv_unit)
    TextView tv_unit;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private List<MinerHarvestBean.DataBeanX.DataBean> dataBeanList;
    private MinerHarvesAdapter minerHarvesAdapter;//矿机算力统计列表

    private OnSelectParameterListener onSelectParameterListener;//参数的选择

    private int factoryId;//矿场id
    private int datetype;//日期类型
    private  String dateString="";//日期




    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_miner_harvest, container, false);
        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
        getdata();

    }

    private void init() {
        EventBus.getDefault().register(this);
        factoryId=onSelectParameterListener.getFactoryId();
        datetype=onSelectParameterListener.getDateType();
        dateString=onSelectParameterListener.getDate();


        dataBeanList = new ArrayList<>();
        minerHarvesAdapter=new MinerHarvesAdapter(getActivity(),dataBeanList);

        lRecyclerViewAdapter = new LRecyclerViewAdapter(minerHarvesAdapter);
        //空白页
        mRecyclerView.setEmptyView(getView().findViewById(R.id.empty));
        getView().findViewById(R.id.tv_refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshClear();

            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(lRecyclerViewAdapter);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); //设置下拉刷新Progress的样式
        // mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);  //设置下拉刷新箭头
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.system_color, R.color.system_color, R.color.bg);
//设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.system_color, R.color.system_color, android.R.color.white);

        //分割线
        DividerDecoration divider = new DividerDecoration.Builder(getActivity())
                .setHeight(R.dimen.d_1)
                .setPadding(R.dimen.d_0)
                .setColorResource(R.color.colorLine)
                .build();
        mRecyclerView.addItemDecoration(divider);
        mRecyclerView.setOnRefreshListener(new OnRefreshListener() {//下拉刷新
            @Override
            public void onRefresh() {

                refreshClear();
            }
        });
        //停止下拉刷新
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadMoreEnabled(false);
        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {//加载更多
            @Override
            public void onLoadMore() {
//                getData(pagenum, false);
            }
        });

    }

    /**
     * 刷新数据
     */
    private void refreshClear() {
        dataBeanList.clear();
        minerHarvesAdapter.notifyDataSetChanged();
        getdata();
    }

    /**
     * 获取网络数据
     */
    private void getdata() {
        if (NetWorkUtil.check(getActivity()) &&factoryId!=0) {


            NetHelperNew.ttCalPoweStatistics_Table_Day(datetype,factoryId+"",dateString,new ApiHttpCallback(getActivity(), 1, true) {
                @Override
                public void onSuccess(String data) {

                    MinerHarvestBean staffBean = MyGson.getInstance().fromJson(data, MinerHarvestBean.class);

                    Log.i("-->","算力表："+data);

                    if (staffBean.getType() == 1) {


                        tv_unit.setText("实际算力("+staffBean.getData().getDw()+")");
                        mRecyclerView.refreshComplete(10);

                        //更新数据
                        dataBeanList.addAll(staffBean.getData().getData());

                        minerHarvesAdapter.notifyDataSetChanged();




                    } else {
                        mRecyclerView.refreshComplete(10);
                        ToastUtil.showToast(getActivity(), staffBean.getContent());
//                        new ErrorDialog(getActivity(), staffBean.getContent()).show();
                    }


                }

            });
        }

    }

    // 当FRagmen被加载到activity的时候会被回调
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (activity instanceof OnSelectParameterListener) {
            onSelectParameterListener = (OnSelectParameterListener) activity; // 2.2 获取到宿主activity并赋值
        } else {
            throw new IllegalArgumentException("activity must implements FragmentInteraction");
        }
    }

    //把传递进来的activity对象释放掉
    @Override
    public void onDetach() {
        super.onDetach();
        onSelectParameterListener = null;
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = false, priority = 0)
    public void handleData(String str) {

        if (onSelectParameterListener!=null&&str.equals("10")) {
            this.datetype=onSelectParameterListener.getDateType();
            this.dateString=onSelectParameterListener.getDate();
            this.factoryId=onSelectParameterListener.getFactoryId();

            refreshClear();


        }


    }
}
